package com.kiln.juxingqs.ui.vm

import android.app.Application
import android.net.Uri
import android.text.TextUtils
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.out_time
import client.global.commlib.toast.toast
import client.global.commlib.utils.Util
import com.kiln.juxingqs.api.bean.UploadRet
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response

class RealAbleDateViewModel(app: Application) : MiddleVm(app) {

    val isShowing = MutableLiveData<Boolean>()
    val startDate = MutableLiveData<String>("")
    val endDate = MutableLiveData<String>("")
    val standard = MutableLiveData<String>("有效期")

    fun swichSta(i: Int) {

        when (i) {

            0 -> {
                standard.value = "有效期"
            }

            else -> {
                standard.value = "长期"
            }
        }

    }

    val call = MutableLiveData<JsonResult<Ret>>()
    fun realnameEnter() {
        if (TextUtils.isEmpty(startDate.value)) {
            toast("请选择日期后提交")
            return
        }

        val c_type = if (standard.value.equals("有效期")) 0 else 1

        val s = startDate.value!!.replace("-", "")
        val e = if (c_type == 0) endDate.value!!.replace("-", "") else ""

        if (c_type == 0) {
            val sData = s.substring(4)
            val eData = e.substring(4)
            if (!sData.equals(eData)) {
                toast("请输入有效的日期")
                return
            }

            val syData = s.substring(0, 4).toInt()
            val eyData = e.substring(0, 4).toInt()
            if (eyData - syData < 5) {
                toast("请输入有效的日期")
                return
            }
        }

        hold({

            server.user_dg_info(
                "${c_type}",
                s,
                e
            )
        }, {
            isShowing.value = true

        }, {
            call.value = it
        }, complete = {
            isShowing.value = false
        })
    }


}